//
//  MyOrderController.m
//  AppNew
//
//  Created by 陈名邦 on 2019/6/14.
//  Copyright © 2019 华伟. All rights reserved.
//

#import "MyOrderController.h"
#import "MyOrderModel.h"
@interface MyOrderController (){
    NSArray *orderData;
}

@end

@implementation MyOrderController

- (void)_loadData:(NSString *)typeStr{
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:typeStr forKey:@"type"];
    NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/trade/getAllOrderByType?type=%@",typeStr];
    [NetRequestTool post:url
                  params:@{}
                 success:^(id  _Nonnull json) {
                     BOOL flag = [[json objectForKey:@"flag"] boolValue];
                     if (flag) {
                         
                         self->orderData = [MyOrderModel mj_objectArrayWithKeyValuesArray:[json objectForKey:@"data"]];
                         [self->_tableView reloadData];
                     }else{
                         
                         [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                         
                     }
                     
                 } failure:^(NSError * _Nonnull error) {
                     
                 }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的订单";
    [self initViews];
    [self _loadData:@"all"];
}

- (void)initViews{
    
    NSArray *titleArr = @[@"全部",@"进行中",@"已完成",@"已取消",@"申述中"];
    CGFloat buttonWidth = ScreenWidth / titleArr.count;
    for (int i = 0; i < titleArr.count; i ++) {
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonWidth * i, 0, buttonWidth, 49.f)];
        if (i == 0) {
            _selectButton = button;
        }
        button.tag = i;
        [button setTitle:titleArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self
                   action:@selector(buttonAction:)
         forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
    }
    
    _selectImg = [[UIImageView alloc] initWithFrame:CGRectMake(buttonWidth / 4 , 47.f, buttonWidth / 2, 2)];
    _selectImg.backgroundColor = BaseColor;
    [self.view addSubview:_selectImg];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 49.f, ScreenWidth, ScreenHeight - NAVBAR_HEIGHT - 49.f)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"MyOrderListCell" bundle:[NSBundle mainBundle]]
     forCellReuseIdentifier:@"MyOrderList_Cell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
}

- (void)buttonAction:(UIButton *)button{
    NSArray *titleArr = @[@"all",@"a",@"b",@"c",@"d"];
    [UIView animateWithDuration:0.3
                     animations:^{
                         
                         CGRect frame = self->_selectImg.frame;
                         frame.origin.x = button.origin.x + button.size.width / 4;
                         self->_selectImg.frame = frame;
                         
                         
                     }];
    _selectButton = button;
    NSInteger index = button.tag;
    
    [self _loadData:titleArr[index]];
    
}

#pragma mark - UITableViewDelegate,UITableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return orderData.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyOrderList_Cell"];
    if (!cell) {
        cell = [[MyOrderListCell alloc] init];
    }
    cell.model = orderData[indexPath.row];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 156.f;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}

@end
